import ErrorCodeConstant from '~/constants/error_code';

interface RespStruct{
    code: number;
    message: string;
    data: any;
}

export function success(message = ErrorCodeConstant.getDesc(ErrorCodeConstant.SUCCESS), data: any = null, code = ErrorCodeConstant.SUCCESS) {
    return result(code, message, data);
}

export function fail(message = ErrorCodeConstant.getDesc(ErrorCodeConstant.FAIL), code = ErrorCodeConstant.FAIL, data: any = null) {
    return result(code, message, data);
}

export function data(data: any = null, code = ErrorCodeConstant.SUCCESS) {
    return result(code, ErrorCodeConstant.getDesc(ErrorCodeConstant.SUCCESS), data);
}

export function result(code: number, message: string, data: any): RespStruct {
    return <RespStruct>{
        code,
        message,
        data
    };
}

export function pageReturn(list: any, count: number, page: number, limit: number) {
    return {
        total: Number(count),
        page_amount: Math.ceil(count / Number(limit)),
        page_num: Number(page),
        page_size: Number(limit),
        list
    };
}